﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using MSD.WL.Site.Models;

namespace MSD.WL.Site.Controllers
{
    [Authorize]
    public class DepartmentTypesController : Controller
    {
        private EMSContext db = new EMSContext();

        // GET: DepartmentTypes
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Add()
        {
            return View();
        }

        //添加数据
        [ValidateAntiForgeryToken]
        [HttpPost]
        public ActionResult Add(string departmenttypecode, string departmenttypename)
        {
            DepartmentType department = new DepartmentType();
            department.DepartmentCode = departmenttypecode;
            department.DepartmentName = departmenttypename;
            db.DepartmentType.Add(department);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        //修改数据
        [HttpPost]
        public JsonResult Edit(string EditArray)
        {
            System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
            var list = js.Deserialize<List<DepartmentType>>(EditArray);
            if (list != null)
            {
                foreach(var item in list)
                {
                    DepartmentType departmenttype = new DepartmentType() { ID = item.ID, DepartmentCode = item.DepartmentCode, DepartmentName = item.DepartmentName };
                    db.Entry(departmenttype).State = EntityState.Modified;
                }
                db.SaveChanges();
                string msg = "isok";
                return Json(msg, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return null;
            }
        }

        //批量删除
        [HttpPost]
        public JsonResult Delete(string idArray)
        {
            System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
            var list = js.Deserialize<List<DepartmentType>>(idArray);
            if (list != null)
            {
                foreach(var i in list)
                {
                    //DepartmentType depart = db.DepartmentType.Find(i.ID);
                    DepartmentType depart = db.DepartmentType.FirstOrDefault(u => u.ID == i.ID);
                    db.DepartmentType.Remove(depart);
                }
                db.SaveChanges();
                string msg = "isok";
                return Json(msg, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return null;
            }
        }

        //单个删除
        [HttpPost]
        public JsonResult DeleteById(int Id)
        {

            //System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
            //int i = Id;
            //DepartmentType departbyId = db.DepartmentType.Find(Id);
            DepartmentType departbyId = db.DepartmentType.FirstOrDefault(u => u.ID == Id);
            if (departbyId != null)
            {
                db.DepartmentType.Remove(departbyId);
            }
            db.SaveChanges();
            string msg = "isok";
            return Json(msg, JsonRequestBehavior.AllowGet);
        }
        //请求json数据
        [HttpPost]
        public JsonResult GetDepartmentTypeList(int limit,int offset)
        {
            string txtNum = Request["txtNum"].Trim();
            string txtName = Request["txtName"].Trim();
            //string txtNum = "6";
            var Departmenttype = db.DepartmentType as IQueryable<DepartmentType>;
            if (!String.IsNullOrEmpty(txtNum))
            {
                Departmenttype = Departmenttype.Where(x => x.DepartmentCode.Contains(txtNum));
            }
            if (!String.IsNullOrEmpty(txtName))
            {
                Departmenttype = Departmenttype.Where(x => x.DepartmentName.Contains(txtName));
            }
            var departmenttype = Departmenttype.Select(x => new { ID = x.ID, DepartmentTypeCode = x.DepartmentCode, DepartmentTypeName = x.DepartmentName }).ToList();

            var total = departmenttype.Count();
            var rows = departmenttype.OrderBy(x=>x.ID).Skip(offset).Take(limit).ToList();
            return Json(new { total=total,rows=rows}, JsonRequestBehavior.AllowGet);
        }
        

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
            
        }
    }
}
